apiVersion: v1
kind: DeploymentConfig
metadata:
  name: "resultsdb-api"
  labels:
    service: api
    app: resultsdb
spec:
  replicas: 2
  selector:
    app: resultsdb
    service: api
  strategy:
    type: Rolling
    rollingParams:
      pre:
        failurePolicy: Abort
        execNewPod:  
          containerName: api
          command:
            - /bin/sh
            - -i
            - -c
            - |
              # try for 10 minutes (600 seconds)
              e=$(( $(date +%s) + 600 ))
              i=0
              while [ $(date +%s) -lt $e ]; do
                echo 'TRY #'$((++i))
                if resultsdb init_db ; then
                  exit 0
                fi
              done
              exit 1
          volumes:
            - config-volume
            - httpd-config-volume
  template:
    metadata:
      labels:
        service: api
        app: resultsdb
    spec:
      containers:
        - name: api
{% if env == 'staging' %}
          image: "{{ stg_resultsdb_image }}"
{% else %}
          image: "{{ prod_resultsdb_image }}"
{% endif %}
          imagePullPolicy: Always
          env:
            - name: RESULTSDB_WSGI_SCRIPT_PATH
              value: "/usr/share/resultsdb/resultsdb.wsgi"
            - name: RESULTSDB_THREADS
              value: "5"
            - name: RESULTSDB_CONF_PATH
              value: "/etc/httpd/conf.d/resultsdb.conf"
            - name: RESULTSDB_LOG_LEVEL
              value: "info"
            - name: RESULTSDB_MESSAGING
              value: "Y"
          ports:
            - containerPort: 5001
          volumeMounts:
            - name: config-volume
              mountPath: /etc/resultsdb
              readOnly: true
            - name: httpd-config-volume
              mountPath: /etc/httpd/conf.d
              readOnly: true
            - name: fedora-messaging-ca-volume
              mountPath: /etc/pki/rabbitmq/ca
              readOnly: true
            - name: fedora-messaging-key-volume
              mountPath: /etc/pki/rabbitmq/key
              readOnly: true
            - name: fedora-messaging-crt-volume
              mountPath: /etc/pki/rabbitmq/crt
              readOnly: true
            - name: fedora-messaging-config-volume
              mountPath: /etc/fedora-messaging
              readOnly: true
          readinessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 5
            httpGet:
              path: /api/v2.0/
              port: 5001
          livenessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 30
            httpGet:
              path: /api/v2.0/
              port: 5001
          # Limit to 384MB memory. This is probably *not* enough but it is
          # necessary in the current environment to allow for 2 replicas and
          # rolling updates, without hitting the (very aggressive) memory quota.
          resources:
            limits:
              memory: 384Mi
              cpu: 0.3
      volumes:
        - name: config-volume
          secret:
            secretName: "resultsdb-config"
        - name: httpd-config-volume
          configMap:
            name: "resultsdb-httpd-config"
        - name: fedora-messaging-config-volume
          configMap:
            name: fedora-messaging-configmap
        - name: fedora-messaging-ca-volume
          secret:
            secretName: resultsdb-fedora-messaging-ca
        - name: fedora-messaging-crt-volume
          secret:
            secretName: resultsdb-fedora-messaging-crt
        - name: fedora-messaging-key-volume
          secret:
            secretName: resultsdb-fedora-messaging-key
  triggers:
    - type: ConfigChange
